package com.base.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.po.voip.ChangeRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.Users;
import com.base.service.ChangeRecordService;
import com.base.service.UsersService;
import com.base.utils.Page;

/**
 * 变更记录
 *
 * @author K
 *
 */
@Controller
@RequestMapping("/admin")
public class content_biangengjilu {
	@Autowired
	private ChangeRecordService changeRecordService;
	@Autowired
	private UsersService usersService;

	// 变更记录
	@RequestMapping("/content_biangengjilu")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<ChangeRecord> changeRecord = null;
//		Users user = null;
		if (adminUsergrade == 0) {
			page.setCurrentPageNo(1);
			page.setRecordTotal(changeRecordService.countByAll(null, null, null, null, null));
			changeRecord = changeRecordService.selectByAll(page, null, null, null, null, null);
		}
		if (adminUsergrade == 1) {
			page.setCurrentPageNo(1);
			page.setRecordTotal(changeRecordService.countByAll(null, adminUserId, null, null, null));
			changeRecord = changeRecordService.selectByAll(page, null, adminUserId, null, null, null);
		}
		/*
		 * 设置分页
		 */
		int totlenum = page.getRecordTotal();// 页面总数
		request.setAttribute("totlenum", totlenum);// 传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

		if(changeRecord != null && changeRecord.size() > 0) {
			for(ChangeRecord c : changeRecord) {
				if(c.getTypeb() == 0) {
					c.setPluschangeb("-"+c.getChangeb().toString());
				}else {
					c.setPluschangeb("+"+c.getChangeb().toString());
				}
                if(c.getTypes() == 0) {
                	c.setPluschanges("-"+c.getChanges().toString());
				}else {
					c.setPluschanges("+"+c.getChanges().toString());
				}

                if(c.getParentid() != null) {
                	Users p = usersService.selectByPrimaryKey(c.getParentid());
                	if(p != null) {
                		c.setParentid(p.getUsername());
                	}else {
                		c.setParentid("代理商不存在");
                	}
                }else {
                	c.setParentid("未查询到代理商");
                }

                if(c.getShopid() != null) {
                	Users s = usersService.selectByPrimaryKey(c.getShopid());
                	if(s != null) {
                		c.setShopid(s.getUsername());
                	}else {
                		c.setShopid("商家不存在");
                	}
                }else {
                	c.setShopid("未查询到商家");
                }
			}
		}

		request.setAttribute("list", changeRecord);
		request.setAttribute("type", 0);
		modelAndView.setViewName("jsp/content_biangengjilu");
		return modelAndView;
	}

	@RequestMapping("/selectChangeRecord")
	public ModelAndView selectChangeRecord(HttpServletRequest request, HttpServletResponse response,
			String parentid, String shopid, String mobile, String timeFrom,
			String timeTo, Integer curPage, Integer type) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		// 返回ModelAndView
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		ModelAndView modelAndView = new ModelAndView();
		PrintWriter out = response.getWriter();
		Page page = new Page();
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<ChangeRecord> changeRecord = null;
//		Users user = null;

		String fParentID = null;
		String fShopID = null;
		String fMobile = null;
		String fTimeFrom = null;
		String fTimeTo = null;

		if(parentid != null && !parentid.equals("")) {
			List<Users> p = usersService.selectByUserName(parentid);
			if(p != null && p.size() > 0) {
				fParentID = p.get(0).getId();
			}else {
				fParentID = null;
			}
		}
		if(shopid != null && !shopid.equals("")) {
			List<Users> s = usersService.selectByUserName(shopid);
			if(s != null && s.size() > 0) {
				fShopID = s.get(0).getId();
			}else {
				fShopID = null;
			}
		}
		if(mobile != null && !mobile.equals("")) {
			fMobile = mobile;
		}
		if(timeFrom !=null && !timeFrom.equals("") && timeTo != null && !timeTo.equals("")) {
			fTimeFrom = timeFrom + " 00:00:00";
			fTimeTo = timeTo + " 23:59:59";
		}else {
			out.print("请填写时间段");
		}
		if (adminUsergrade == 0) {
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(changeRecordService.countByAll(fMobile, fParentID, fShopID, fTimeFrom, fTimeTo));
			changeRecord = changeRecordService.selectByAll(page, fMobile, fParentID, fShopID, fTimeFrom, fTimeTo);
		}
		if (adminUsergrade == 1) {
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(changeRecordService.countByAll(fMobile, adminUserId, fShopID, fTimeFrom, fTimeTo));
			changeRecord = changeRecordService.selectByAll(page, fMobile, adminUserId, fShopID, fTimeFrom, fTimeTo);
		}

		/*
		 * 设置分页
		 */
		int totlenum = page.getRecordTotal();// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

		if(changeRecord != null && changeRecord.size() > 0) {
			for(ChangeRecord c : changeRecord) {
				if(c.getTypeb() == 0) {
					c.setPluschangeb("-"+c.getChangeb().toString());
				}else {
					c.setPluschangeb("+"+c.getChangeb().toString());
				}
                if(c.getTypes() == 0) {
                	c.setPluschanges("-"+c.getChanges().toString());
				}else {
					c.setPluschanges("+"+c.getChanges().toString());
				}

                if(c.getParentid() != null) {
                	Users p = usersService.selectByPrimaryKey(c.getParentid());
                	if(p != null) {
                		c.setParentid(p.getUsername());
                	}else {
                		c.setParentid("代理商不存在");
                	}
                }else {
                	c.setParentid("未查询到代理商");
                }

                if(c.getShopid() != null) {
                	Users s = usersService.selectByPrimaryKey(c.getShopid());
                	if(s != null) {
                		c.setShopid(s.getUsername());
                	}else {
                		c.setShopid("商家不存在");
                	}
                }else {
                	c.setShopid("未查询到商家");
                }
			}
		}

		// 设置页面进行跳转
		modelAndView.setViewName("jsp/content_biangengjilu");
		request.setAttribute("parentid", parentid);
		request.setAttribute("shopid", shopid);
		request.setAttribute("mobile", mobile);
		request.setAttribute("list", changeRecord);
		request.setAttribute("timeFrom", timeFrom);
		request.setAttribute("timeTo", timeTo);
		request.setAttribute("type", type);
		return modelAndView;
	}
}
